package com.difsonapps.android.marker.beans;

import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class Friend {
	int id;
	User userTo;
	User userFrom;
	String createdAt;
	int confirmed;
	String confirmAt;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public User getUserTo() {
		return userTo;
	}
	public void setUserTo(User userTo) {
		this.userTo = userTo;
	}
	public User getUserFrom() {
		return userFrom;
	}
	public void setUserFrom(User userFrom) {
		this.userFrom = userFrom;
	}
	public String getCreatedAt() {
		return createdAt;
	}
	public void setCreatedAt(String createdAt) {
		this.createdAt = createdAt;
	}
	public int getConfirmed() {
		return confirmed;
	}
	public void setConfirmed(int confirmed) {
		this.confirmed = confirmed;
	}
	public String getConfirmAt() {
		return confirmAt;
	}
	public void setConfirmAt(String confirmAt) {
		this.confirmAt = confirmAt;
	}
	
	public boolean isConfirmed(){
		return confirmed == 1;
	}
	public boolean isWaiting(){
		return confirmed == 0;
	}
	public boolean isDeclined(){
		return confirmed == 2;
	}
	
	
	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Friend other = (Friend) obj;
		if (id != other.id)
			return false;
		return true;
	}
	public static Friend parseFromJsonObject(JSONObject json){
		if(json==null || !json.has("id")) return null;
		Friend result = new Friend();
		try {
			result.setId(json.getInt("id"));
			result.setConfirmed(json.getInt("confirmed"));
			result.setConfirmAt(json.getString("created_at"));
			result.setCreatedAt(json.getString("created_at"));
			if(json.has("user-from")) result.setUserFrom(User.parseFromJsonObject(json.getJSONObject("user-from")));
			if(json.has("user-to")) result.setUserTo(User.parseFromJsonObject(json.getJSONObject("user-to")));
		} catch (JSONException e) {
			e.printStackTrace();
			Log.e("Parse Friend From JSON", e.getMessage());
			return null;
		}
		return result;
	}
}
